package com.mtons.mblog.web.controller.site.user;

import com.mtons.mblog.base.lang.Consts;
import com.mtons.mblog.base.lang.Result;
import com.mtons.mblog.modules.pojo.UserFollowRel;
import com.mtons.mblog.modules.rabbitmq.FollowMqMessage;
import com.mtons.mblog.modules.rabbitmq.RabbitConstant;
import com.mtons.mblog.modules.service.PostService;
import com.mtons.mblog.modules.service.UserFollowService;
import com.mtons.mblog.modules.vo.AccountProfile;
import com.mtons.mblog.modules.vo.UserVO;
import com.mtons.mblog.web.controller.BaseController;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName: FollowController
 * @Auther: Jerry
 * @Date: 2020/5/21 17:06
 * @Desctiption: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/user")
public class FollowController extends BaseController {

    @Autowired
    private UserFollowService userFollowService;

    @Autowired
    private PostService postService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/follow")
    public Result follow(Long followId) {
        AccountProfile profile = getProfile();
        if ( followId == profile.getId()) {
            return Result.failure( "亲，您不能关注您自己！");
        }
        if (userFollowService.isFollowed(followId, profile)) {
            return Result.failure( "您已经关注！");
        }
        userFollowService.follow(followId, profile);
        sendMessage(followId, profile.getId());
        return Result.success( "感谢亲的关注！");
    }

    @RequestMapping("/unFollow")
    public Result unFollow(Long followId){
        AccountProfile profile = getProfile();
        if (!userFollowService.isFollowed(followId, profile)) {
            return Result.failure( "您已经取消关注！");
        }
        userFollowService.unFollow(followId, profile);
        return Result.success("取消关注") ;
    }

    @RequestMapping("/isFollowed")
    public Result isFollowed(Long id){
        UserVO author = postService.get(id).getAuthor();
        AccountProfile profile = getProfile();
        boolean isFollowed = false;
        if (author != null && profile !=null)
        {
            isFollowed = userFollowService.isFollowed(author.getId(), profile);
        }
        return Result.success(isFollowed) ;
    }

    @GetMapping("/myFollow")
    public Result<List<UserFollowRel>> myFollows() {
        AccountProfile profile = getProfile();
        List<UserFollowRel> followUserResp = userFollowService.myFollows(profile.getId());
        return Result.success(followUserResp);
    }

    @GetMapping("/myFans")
    public Result<List<UserFollowRel>> myFans() {
        AccountProfile profile = getProfile();
        List<UserFollowRel> followUserResp = userFollowService.myFans(profile.getId());
        return Result.success(followUserResp);
    }

    @GetMapping("/myFollowCount")
    public Result<Long> myFollowCount(@RequestParam (value = "targetUserId", required = false) Long targetUserId){
        Long count = userFollowService.myFollowCount(targetUserId);
        return Result.success(count);
    }


    @GetMapping("/myFansCount")
    public Result<Long> myFansCount(@RequestParam (value = "targetUserId", required = false) Long targetUserId) {
        Long count = userFollowService.myFansCount(targetUserId);
        return Result.success(count);
    }

    /**
     * 发送通知
     * @param followId  被关注者id
     * @param userId  关注者id
     */
    private void sendMessage(long followId, long userId) {
        rabbitTemplate.convertAndSend(RabbitConstant.FOLLOW_EXCHAGE, RabbitConstant.FOLLOW_ROUTING_KEY,
                new FollowMqMessage(userId, followId, Consts.MESSAGE_EVENT_FOLLOW_USER));
    }
}
